package net.xdclass.component;

import lombok.extern.slf4j.Slf4j;
import net.xdclass.enums.ProductOrderPayTypeEnum;
import net.xdclass.vo.PayInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author : zhen.xue
 * @date : 2021-07-06 22:23
 */
@Slf4j
@Component
public class PayFactory {

    @Autowired
    private AlipayStrategy alipayStrategy;

    @Autowired
    private WechatPayStrategy wechatPayStrategy;

    /**
     * 创建支付简单工厂模式
     * @param payInfoVO
     * @return
     */
    public String pay(PayInfoVO payInfoVO){
        String payType = payInfoVO.getPayType();
        if(ProductOrderPayTypeEnum.ALIPAY.name().equalsIgnoreCase(payType)){
            PayStrategyContext context = new PayStrategyContext(alipayStrategy);
            return context.executeUnifiedOrder(payInfoVO);
        }else if(ProductOrderPayTypeEnum.WECHAT.name().equalsIgnoreCase(payType)){
            PayStrategyContext context = new PayStrategyContext(wechatPayStrategy);
            return context.executeUnifiedOrder(payInfoVO);
        }
        return "";
    }


    /**
     * 查询订单
     * 支付成功返回非空，支付成功返回空
     * @param payInfoVO
     * @return
     */
    public String queryPaySuccess(PayInfoVO payInfoVO){
        String payType = payInfoVO.getPayType();
        if(ProductOrderPayTypeEnum.ALIPAY.name().equalsIgnoreCase(payType)){
            PayStrategyContext context = new PayStrategyContext(alipayStrategy);
            return context.executeQueryPaySuccess(payInfoVO);
        }else if(ProductOrderPayTypeEnum.WECHAT.name().equalsIgnoreCase(payType)){
            PayStrategyContext context = new PayStrategyContext(wechatPayStrategy);
            return context.executeQueryPaySuccess(payInfoVO);
        }
        return "";
    }
}